//
//  ClippingViewController.m
//  Drops
//
//  Created by dllo on 16/5/26.
//  Copyright © 2016年 Drops.er. All rights reserved.
//

#import "ClippingViewController.h"
#import "ClippingTool.h"
#define WWWW [UIScreen mainScreen].bounds.size.width
#define HHHH [UIScreen mainScreen].bounds.size.height
@interface ClippingViewController ()
{
    ClippingTool *_clippingTool;
}
@end

@implementation ClippingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(clickRightButton:)];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 50, 40);
    [leftBtn setImage:[UIImage imageNamed:@"back.png"] forState:0];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    [leftBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 50, 40);
    [rightBtn setImage:[UIImage imageNamed:@"done.png"] forState:0];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    [rightBtn addTarget:self action:@selector(clickRightButton:) forControlEvents:UIControlEventTouchUpInside];

    
    self.BScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 10, WWWW, HHHH - 184)];
    self.BScrollView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.BScrollView];
    
    self.editImgV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, WWWW - 20, HHHH - 184)];
    [self.BScrollView addSubview:self.editImgV];
    self.editImage = [self fixOrientation:self.editImage];
    self.editImgV.image = self.editImage;
    self.editImgV.contentMode = UIViewContentModeScaleAspectFit;
    self.shearScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, HHHH - 164, WWWW, 100)];
    self.shearScrollView.backgroundColor = [UIColor grayColor];
    self.shearScrollView.alpha = 0;
    [UIView animateWithDuration:1.0 animations:^{
    self.shearScrollView.alpha = 1;
    }];
    [self.view addSubview:self.shearScrollView];
    _clippingTool = [[ClippingTool alloc]init];
    _clippingTool.bScrollview = self.BScrollView;
    _clippingTool.showImgV = self.editImgV;
    _clippingTool.mScrollview = self.shearScrollView;
    _clippingTool.bView = self.view;
    [_clippingTool setup];
}

-(void)clickRightButton:(UIButton *)button{
    [_clippingTool execute] ;
    self.clippingBlock(self.editImgV.image);
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 图像方向修正
- (UIImage *)fixOrientation:(UIImage *)aImage {
    
    // 无操作如果取向已经正确
    if (aImage.imageOrientation == UIImageOrientationUp){
        return aImage;
    }else{
    
    // 我们需要计算合适的变换使图片放正。
    // 我们用2步骤:如果左/右/向下旋转,然后镜像翻转。
    CGAffineTransform transform = CGAffineTransformIdentity;
    
    switch (aImage.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
            
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
            
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        default:
            break;
    }
    
    switch (aImage.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
            
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        default:
            break;
    }
    
    // 现在我们把底层CGImage到一个新的背景下,应用变换
    CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,CGImageGetBitsPerComponent(aImage.CGImage), 0,CGImageGetColorSpace(aImage.CGImage),CGImageGetBitmapInfo(aImage.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (aImage.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
            break;
        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
            break;
    }
    
    // 现在我们创建一个新的用户界面图像从绘图上下文
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
    }
}

- (void)backAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
